#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>

#define MAXN 10005
using namespace std;

int n, m;
int dfn[MAXN], low[MAXN], color[MAXN], col[MAXN];
//bool vis[MAXN];
vector <int> G[MAXN];
int sum, deep;
stack<int> s;

void tarjan (int u) {
	deep++;
	dfn[u] = deep;
	low[u] = deep;
	//vis[u] = 1;
	s.push(u);
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else {
			if (!color[v]) {
				low[u] = min(low[u], low[v]);
			}
		}
	}
	if (dfn[u] == low[u]) {
		//vis[u] = 0;
		sum++;
		color[u] = sum;
		while (s.top() != u) {
			color[s.top()] = sum;
			s.pop();
		}
		s.pop();
	}
}
int main() {
	scanf ("%d%d", &n, &m);
	int x, y, ans = 0;
	for (int i = 1; i <= m; ++i) {
		scanf ("%d%d", &x, &y);
		G[x].push_back(y);
	}
	for (int i = 1; i <= n; ++i) {
		if (!dfn[i]) {
			tarjan(i);
		}
	}
	for (int i = 1; i <= n; ++i) {
		++col[color[i]];
	}
	for (int i = 1; i <= sum; ++i) {
		if (col[i] > 1) {
			ans++;
		}
	} 
	printf ("%d\n", ans);
	return 0;
}